package ils.domain.issue.model;

import ils.util.HibernateSession;
import ils.util.HibernateSessionManager;
import ils.util.SpringUtil;
import ils.util.Util;
import junit.framework.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: rejeevd
 * Date: 23/09/12
 * Time: 16:07
 * To change this template use File | Settings | File Templates.
 */
public class IssueRepositoryTest
{
    private HibernateSessionManager hibernateSessionManager = new HibernateSessionManager();
    ApplicationContext context = SpringUtil.getApplicationContext();
    IssueRepository issueRepository = (IssueRepository)context.getBean("issueRepository");
    ItemRepository itemRepository = (ItemRepository)context.getBean("itemRepository");

    @Test
    public void testCreateIssue()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            Issue issue = new Issue();
            issue.setUserId(3);
            Item item = itemRepository.getItem(1);
            issue.setItem(item);
            issue.setDueDate(new Date(System.currentTimeMillis() + Util.DAY * 7));
            issue = issueRepository.createIssue(issue);
            Assert.assertNotNull(issue.getId());
        }
    }

    @Test
    public void testGetIssue()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            Issue issue = issueRepository.getIssue(1);
            Assert.assertNotNull(issue);
            issue = issueRepository.getIssue(34543);
            Assert.assertNull(issue);
        }
    }

    @Test
    public void testGetIssueByBarcode()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            Issue issue = issueRepository.getIssue("11111");
            Assert.assertNotNull(issue);
            issue = issueRepository.getIssue("ecvfr");
            Assert.assertNull(issue);
        }
    }

    @Test
    public void testGetIssuesForUser()
    {
        try(HibernateSession session  = hibernateSessionManager.getSession())
        {
            List<Issue> openIssuesForUser = issueRepository.getOpenIssuesForUser(3);
            Assert.assertTrue("number of issues should be more than 0", openIssuesForUser.size() > 0);
            for(Issue issue : openIssuesForUser)
            {
                Assert.assertEquals(3, (int)issue.getUserId());
            }
        }
    }

    @Test
    public void testGetOpenIssues()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            List<Issue> openIssues = issueRepository.getOpenIssues(0, 20);
            Assert.assertTrue("number of issues should be more than 0", openIssues.size() > 0);
            for(Issue issue : openIssues)
            {
                Assert.assertNull(issue.getReturnDate());
            }
        }
    }

    @Test
    public void testEditIssues()
    {
        try(HibernateSession session = hibernateSessionManager.getSession())
        {
            Issue issue = new Issue();
            issue.setUserId(3);
            Item item = itemRepository.getItem(1);
            issue.setItem(item);
            issue.setDueDate(new Date(System.currentTimeMillis() + Util.DAY * 7));
            issue = issueRepository.createIssue(issue);
            Assert.assertNotNull(issue.getId());
            issue.setReturnDate(new Date());
            issue = issueRepository.editIssue(issue);
        }
    }
}
